<?php
require '../utils/conn.php';
include '../models/Question.php';
include '../models/Answer.php';
function getAllQuestions($page, $count) {
	$start = $count * ($page - 1);
	$query = "SELECT * FROM question ORDER BY question_id DESC limit $start, $count";
	$result = mysql_query($query);
	$questions = array();
	while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
	{
		$question = new Question();
		$question->setQuestionId($row['question_id']);
		$question->setTitle($row['title']);
		$question->setContent($row['content']);
		$question->setTimestamp($row['timestamp']);
		$answerQuery = "select * from answer join question_answer on (answer.answer_id=question_answer.answer_id) where question_answer.question_id=".$question->questionId." ORDER BY qa_id DESC";
		$answerResult = mysql_query($answerQuery);
		$answers = array();
		while ($answerRow = mysql_fetch_array($answerResult,MYSQL_ASSOC))
		{
			$answer = new Answer();
			$answer->setAnswerId($answerRow['answer_id']);
			$answer->setIntroduce($answerRow['introduce']);
			$answer->setContent($answerRow['content']);
			$answer->setNickname($answerRow['nickname']);
			$answer->setTimestamp($answerRow['timestamp']);
			$answers[] = $answer;
		}
		$question->setAnswers($answers);
		$questions[] = $question;
	}
	return $questions;
}

function getCountOfQuestion() {
	$query = "SELECT count(*) as total FROM question";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	return $row['total'];
}

function getQuestionById($questionId){
	$query = "SELECT * FROM question WHERE question_id=$questionId";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$question = new Question();
	$question->setQuestionId($row['question_id']);
	$question->setTitle($row['title']);
	$question->setContent($row['content']);
	$question->setTimestamp($row['timestamp']);
	return  $question;
}

function addAnswer($answer, $questionId) {
	$timestamp = time();
	$query = "INSERT INTO answer(introduce, content, nickname, timestamp) VALUES('$answer->introduce', '$answer->content', '$answer->nickname', '$timestamp')";
	$result = mysql_query($query) or die("插入答案出现错误：". mysql_error());
	$answer->answerId = mysql_insert_id();
	
	$query = "INSERT INTO question_answer(question_id, answer_id) VALUES($questionId, $answer->answerId)";
	$result = mysql_query($query) or die("插入答案出现错误：". mysql_error());
}

function deleteQuestion($questionId) {
	$query = "SELECT answer_id FROM question_answer WHERE question_id = $questionId";
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$answerId = $row['answer_id'];
		deleteAnswer($answerId);
	}
	$deleteQuery = "DELETE FROM question WHERE question_id = $questionId";
	$result = mysql_query($deleteQuery);
}

function deleteAnswer($answerId) {
	$deleteAnswerQuery = "DELETE FROM question_answer WHERE answer_id = $answerId";
	$result = mysql_query($deleteAnswerQuery);
	$deleteQuery = "DELETE FROM answer WHERE answer_id = $answerId";
	$result = mysql_query($deleteQuery);;
}

?>